Skip to content

hir_analysis: add missing sizedness bounds #142712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

davidtwco
Copy link
Member

Default sizedness bounds were not being added to explicit_super_predicates_of and explicit_implied_predicates_of which meant that a trait bound added to a associated type projection would be missing the implied predicate of the default sizedness supertrait of that trait.

An unexpected consequence of this change was that the check for multiple principals was now finding an additional MetaSized principal when eagerly expanding trait aliases - this required modifying lowering to no longer add default bounds to trait aliases.

@rustbot
Copy link
Collaborator

rustbot commented Jun 19, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jun 19, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 19, 2025

HIR ty lowering was modified

cc @fmease

@rust-log-analyzer

This comment has been minimized.

@davidtwco
Copy link
Member Author

Wasn't expecting that failure, looking into it.

@davidtwco
Copy link
Member Author

davidtwco commented Jun 19, 2025

A smaller example of one of the failing crates (which happens with a stage two build):

#![crate_type = "lib"]

use std::marker::PhantomData;

pub trait ZeroMapKV<'a> {
    type Container;
}

pub trait ZeroFrom<'zf, C: ?Sized> {}

pub struct ZeroMap<'a, K: ZeroMapKV<'a>>(PhantomData<&'a K>);

impl<'zf, 's, K> ZeroFrom<'zf, ZeroMap<'s, K>> for ZeroMap<'zf, K>
where
    K: for<'b> ZeroMapKV<'b>,
    <K as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K as ZeroMapKV<'s>>::Container>,
{
}
error[E0308]: mismatched types
  --> f.rs:16:39
   |
16 |     <K as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K as ZeroMapKV<'s>>::Container>,
   |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected trait `<<K as ZeroMapKV<'s>>::Container as MetaSized>`
              found trait `<<K as ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'zf` as defined here...
  --> f.rs:13:6
   |
13 | impl<'zf, 's, K> ZeroFrom<'zf, ZeroMap<'s, K>> for ZeroMap<'zf, K>
   |      ^^^
note: ...does not necessarily outlive the lifetime `'s` as defined here
  --> f.rs:13:11
   |
13 | impl<'zf, 's, K> ZeroFrom<'zf, ZeroMap<'s, K>> for ZeroMap<'zf, K>
   |           ^^

error[E0308]: mismatched types
  --> f.rs:16:39
   |
16 |     <K as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K as ZeroMapKV<'s>>::Container>,
   |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected trait `<<K as ZeroMapKV<'s>>::Container as MetaSized>`
              found trait `<<K as ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'s` as defined here...
  --> f.rs:13:11
   |
13 | impl<'zf, 's, K> ZeroFrom<'zf, ZeroMap<'s, K>> for ZeroMap<'zf, K>
   |           ^^
note: ...does not necessarily outlive the lifetime `'zf` as defined here
  --> f.rs:13:6
   |
13 | impl<'zf, 's, K> ZeroFrom<'zf, ZeroMap<'s, K>> for ZeroMap<'zf, K>
   |      ^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

@petrochenkov
Copy link
Contributor

r? types

@rustbot rustbot added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Jun 19, 2025
@rustbot rustbot assigned jackh726 and unassigned petrochenkov Jun 19, 2025
@compiler-errors
Copy link
Member

I'd like to think about this...

Can you remind me, is trait Foo {} actually trait Foo: MetaSized {}?

@davidtwco
Copy link
Member Author

I'd like to think about this...

Can you remind me, is trait Foo {} actually trait Foo: MetaSized {}?

Yes, it is

Default sizedness bounds were not being added to
`explicit_super_predicates_of` and `explicit_implied_predicates_of`
which meant that a trait bound added to a associated type projection
would be missing the implied predicate of the default sizedness
supertrait of that trait.

An unexpected consequence of this change was that the check for multiple
principals was now finding an additional `MetaSized` principal when
eagerly expanding trait aliases. Instead of special-casing trait aliases
as different from traits and not adding a `MetaSized` supertrait to trait
aliases, filter out `MetaSized` when lowering `dyn Trait`.
@compiler-errors
Copy link
Member

compiler-errors commented Jun 27, 2025

I think this highlights a problematic interaction with trait aliases, and I'm tempted to say that trait aliases should have their behavior reworked to stop expanding into their bounds in dyn types. This would, for example, also fix dyn TraitAlias when trait TraitAlias = A + B, but that's a bigger change to advocate for I guess.

FOr now, I think it's inconsistent to not add MetaSized to trait aliases but add them to traits, and I think instead, to preserve/fix the tests, we should probably filter out MetaSized when we elaborate trait aliases when lowering dyn TraitAlias. This behavior should probably be tagged with a FIXME though.

@davidtwco davidtwco force-pushed the sized-hierarchy-missing-default-bounds branch from 60d5044 to 7684842 Compare June 27, 2025 16:09
@davidtwco
Copy link
Member Author

I think this highlights a problematic interaction with trait aliases, and I'm tempted to say that trait aliases should have their behavior reworked to stop expanding into their bounds in dyn types. This would, for example, also fix dyn TraitAlias when trait TraitAlias = A + B, but that's a bigger change to advocate for I guess.

FOr now, I think it's inconsistent to not add MetaSized to trait aliases but add them to traits, and I think instead, to preserve/fix the tests, we should probably filter out MetaSized when we elaborate trait aliases when lowering dyn TraitAlias. This behavior should probably be tagged with a FIXME though.

Changed to this, thanks.


Still hitting the stage two failure, though I've added a UI test with my reproduction so that will fail first, not had much progress on working out what's going wrong with it.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-miri failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling crc32fast v1.4.2
error[E0308]: mismatched types
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:96:39
   |
96 |     <K as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K as ZeroMapKV<'s>>::Container>,
   |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected trait `<<K as kv::ZeroMapKV<'s>>::Container as MetaSized>`
              found trait `<<K as kv::ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'zf` as defined here...
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:92:6
   |
92 | impl<'zf, 's, K, V> ZeroFrom<'zf, ZeroMap<'s, K, V>> for ZeroMap<'zf, K, V>
   |      ^^^
note: ...does not necessarily outlive the lifetime `'s` as defined here
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:92:11
   |
92 | impl<'zf, 's, K, V> ZeroFrom<'zf, ZeroMap<'s, K, V>> for ZeroMap<'zf, K, V>
   |           ^^

error[E0308]: mismatched types
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:96:39
   |
96 |     <K as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K as ZeroMapKV<'s>>::Container>,
   |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected trait `<<K as kv::ZeroMapKV<'s>>::Container as MetaSized>`
              found trait `<<K as kv::ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'s` as defined here...
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:92:11
   |
92 | impl<'zf, 's, K, V> ZeroFrom<'zf, ZeroMap<'s, K, V>> for ZeroMap<'zf, K, V>
   |           ^^
note: ...does not necessarily outlive the lifetime `'zf` as defined here
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:92:6
   |
92 | impl<'zf, 's, K, V> ZeroFrom<'zf, ZeroMap<'s, K, V>> for ZeroMap<'zf, K, V>
   |      ^^^

error[E0308]: mismatched types
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:112:40
    |
112 |     <K0 as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K0 as ZeroMapKV<'s>>::Container>,
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
    |
    = note: expected trait `<<K0 as kv::ZeroMapKV<'s>>::Container as MetaSized>`
               found trait `<<K0 as kv::ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'zf` as defined here...
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:107:6
    |
107 | impl<'zf, 's, K0, K1, V> ZeroFrom<'zf, ZeroMap2d<'s, K0, K1, V>> for ZeroMap2d<'zf, K0, K1, V>
    |      ^^^
note: ...does not necessarily outlive the lifetime `'s` as defined here
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:107:11
    |
107 | impl<'zf, 's, K0, K1, V> ZeroFrom<'zf, ZeroMap2d<'s, K0, K1, V>> for ZeroMap2d<'zf, K0, K1, V>
    |           ^^

error[E0308]: mismatched types
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:112:40
    |
112 |     <K0 as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K0 as ZeroMapKV<'s>>::Container>,
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
    |
    = note: expected trait `<<K0 as kv::ZeroMapKV<'s>>::Container as MetaSized>`
               found trait `<<K0 as kv::ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'s` as defined here...
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:107:11
    |
107 | impl<'zf, 's, K0, K1, V> ZeroFrom<'zf, ZeroMap2d<'s, K0, K1, V>> for ZeroMap2d<'zf, K0, K1, V>
    |           ^^
note: ...does not necessarily outlive the lifetime `'zf` as defined here
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:107:6
    |
107 | impl<'zf, 's, K0, K1, V> ZeroFrom<'zf, ZeroMap2d<'s, K0, K1, V>> for ZeroMap2d<'zf, K0, K1, V>
    |      ^^^

[RUSTC-TIMING] crc32fast test:false 0.513
   Compiling serde v1.0.219
[RUSTC-TIMING] build_script_build test:false 0.358
   Compiling rustc-literal-escaper v0.0.4
[RUSTC-TIMING] rustc_literal_escaper test:false 0.492
error[E0521]: borrowed data escapes outside of associated function
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/yoke_impls.rs:138:20
    |
107 | unsafe impl<'a, K, V> Yokeable<'a> for ZeroMap<'static, K, V>
    |             -- lifetime `'a` defined here
...
136 |     unsafe fn make(from: Self::Output) -> Self {
    |                    ----
    |                    |
    |                    `from` declared here, outside of the associated function body
    |                    `from` is a reference that is only valid in the associated function body
137 |         debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
138 |         let from = mem::ManuallyDrop::new(from);
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                    |
    |                    `from` escapes the associated function body here
    |                    argument requires that `'a` must outlive `'static`
    |
    = note: requirement occurs because of the type `ZeroMap<'_, K, V>`, which makes the generic argument `'_` invariant
    = note: the struct `ZeroMap<'a, K, V>` is invariant over the parameter `'a`
    = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

error[E0521]: borrowed data escapes outside of associated function
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/yoke_impls.rs:232:20
    |
199 | unsafe impl<'a, K0, K1, V> Yokeable<'a> for ZeroMap2d<'static, K0, K1, V>
    |             -- lifetime `'a` defined here
...
230 |     unsafe fn make(from: Self::Output) -> Self {
    |                    ----
    |                    |
    |                    `from` declared here, outside of the associated function body
    |                    `from` is a reference that is only valid in the associated function body
231 |         debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
232 |         let from = mem::ManuallyDrop::new(from);
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                    |
    |                    `from` escapes the associated function body here
    |                    argument requires that `'a` must outlive `'static`
    |
    = note: requirement occurs because of the type `ZeroMap2d<'_, K0, K1, V>`, which makes the generic argument `'_` invariant
    = note: the struct `ZeroMap2d<'a, K0, K1, V>` is invariant over the parameter `'a`
    = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

error: lifetime may not live long enough
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:101:19
    |
92  | impl<'zf, 's, K, V> ZeroFrom<'zf, ZeroMap<'s, K, V>> for ZeroMap<'zf, K, V>
    |      ---  -- lifetime `'s` defined here
    |      |
    |      lifetime `'zf` defined here
...
101 |             keys: K::Container::zero_from(&other.keys),
    |                   ^^^^^^^^^^^^^^^^^^^^^^^ requires that `'zf` must outlive `'s`
    |
    = help: consider adding the following bound: `'zf: 's`

error: lifetime may not live long enough
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:118:20
    |
107 | impl<'zf, 's, K0, K1, V> ZeroFrom<'zf, ZeroMap2d<'s, K0, K1, V>> for ZeroMap2d<'zf, K0, K1, V>
    |      ---  -- lifetime `'s` defined here
    |      |
    |      lifetime `'zf` defined here
...
118 |             keys0: K0::Container::zero_from(&other.keys0),
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'zf` must outlive `'s`
    |
    = help: consider adding the following bound: `'zf: 's`

Some errors have detailed explanations: E0308, E0521.
For more information about an error, try `rustc --explain E0308`.
[RUSTC-TIMING] zerovec test:false 2.411
error: could not compile `zerovec` (lib) due to 8 previous errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants